import axios from 'axios'


export default function ajax(url,data={},type="get") {
    return new Promise((resolve,reject)=>{
        let promise
        if (type=="get") {//get请求
            var dataStr = ''
            Object.keys(data).forEach(key=>{
                dataStr+= key+'='+data[key]+'&'
            })
            if(dataStr!==''){//表示有参数
                 var index = dataStr.lastIndexOf('&') //去掉最后一个&符号
                dataStr = dataStr.substring(0,index)
                url=url+'?'+dataStr
            }
              promise =  axios.get(url)
        }else { //post请求
           promise = axios.post(url,data)
        }
        promise.then(res=>{
            resolve(res.data)
        }).catch(err=>{
            reject(err)
        })
    })
}